/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.NguoiDung;
import util.HibernateUtil;

/**
 *
 * @author ANNM
 */
public class NhanVienDAO {
     public static NguoiDung Login(String user,String pass){
        int kq=0;
        NguoiDung ds= null;
      
       String tinhtrang="true";

         
          Session session = HibernateUtil.getSessionFactory().openSession();
        
        try {
           String hql="select nd from NguoiDung nd where nd.tinhtrang="+tinhtrang+" and nd.taiKhoan=:user and nd.matKhau=:pass" ;
              //hsx = (HangSanXuat) session.get(HangSanXuat.class, tenHangSX);
            Query query=session.createQuery(hql);
            query.setString("user", user);
            query.setString("pass", pass);
            ds=(NguoiDung)query.uniqueResult();
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;

    }
    public static List<NguoiDung> LayDS( int mach){
        List<NguoiDung> ds= null;
         
        Session session=HibernateUtil.getSessionFactory().openSession();
        String tinhtrang="true";
        String hql="select nd from NguoiDung nd where nd.cuaHang.maCuaHang="+mach;
        Query query=session.createQuery(hql);
        ds=query.list();
        session.close();
     
        return ds;
    }
   
    public static int ThemNV(NguoiDung nv){
        Session session= HibernateUtil.getSessionFactory().openSession();
        Transaction tran=null;
        int kq=1;
        try{
        tran= session.beginTransaction();
        session.saveOrUpdate(nv);
        tran.commit();
        }catch(HibernateException ex){
            tran.rollback();
            System.err.println(ex);
            kq=0;
        }finally{
            session.close();
        }
        return kq;
    }
//    public static int XoaNV(NguoiDung nv){
//         int kq=1;
//          Session session= HibernateUtil.getSessionFactory().openSession();
//        Transaction tran=null;
//        
//        try{
//        tran= session.beginTransaction();
//        session.saveOrUpdate(nv);
//        tran.commit();
//        }catch(HibernateException ex){
//            tran.rollback();
//            System.err.println(ex);
//            kq=0;
//        }finally{
//            session.close();
//        }
//        return kq;
//         
//     }
    public static int ktrauser(String user){
        int kq=1;
        Session ss=HibernateUtil.getSessionFactory().openSession();
        String tinhtrang="true";
        String hql="select nd from NguoiDung nd where nd.taiKhoan= '"+user+ "' ";
        Query query=ss.createQuery(hql);
        List<NguoiDung> nd=query.list();
        if(nd.size() !=0){
            kq=0;
        }
        return kq;
    }
    public static boolean xoaNV(NguoiDung user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
       
        if (user == null) {
            return false;
        }
        session.beginTransaction();
        session.delete(user);
        session.getTransaction().commit();
        session.close();
        return true;
    }
     public static void capNhatThongTinSanPham(NguoiDung nd) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(nd);
        session.getTransaction().commit();
        session.close();
    }
    
}
